﻿#region Using
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion Using

namespace LloydGame
{
    class ScreenSettings : GameScreen
    {

        #region Fields
        protected GameScreenComponent m_menu_Back,
                                      m_setting_gfx_motion_blur,
                                      m_setting_gfx_bloom,
                                      m_setting_gfx_ssao,
                                      m_setting_gfx_fps,
                                      m_setting_gfx_fullscreen;
        #endregion Fields

        #region Properties
        #endregion Properties

        #region Constructor
        public ScreenSettings(Game p_game, GameScreen p_owner)
            : base(p_game, "Settings")
        {
            m_setting_gfx_motion_blur = new ScreenComponentSwitch("Motion blur", ((Game1)p_game).settingGFXMotionBlur, new Vector2(100, 230), new Vector2(240, 32), p_game);
            m_setting_gfx_bloom = new ScreenComponentSwitch("Bloom effect", ((Game1)p_game).settingGFXBloom, new Vector2(100, 260), new Vector2(240, 32), p_game);
            m_setting_gfx_ssao = new ScreenComponentSwitch("Ambient Occlusion", ((Game1)p_game).settingGFXSSAO, new Vector2(100, 290), new Vector2(240, 32), p_game);
            m_setting_gfx_fps = new ScreenComponentSwitch("FPS Counter", ((Game1)p_game).settingGFXFPS, new Vector2(100, 320), new Vector2(240, 32), p_game);
            m_setting_gfx_fullscreen = new ScreenComponentSwitch("Fullscreen", ((Game1)p_game).settingGFXFS, new Vector2(100, 350), new Vector2(240, 32), p_game);
            m_menu_Back = new GameScreenComponent("Back", new Vector2(80, 400), new Vector2(240, 32), p_game);

            m_selectable_components.Add(m_setting_gfx_motion_blur);
            m_selectable_components.Add(m_setting_gfx_bloom);
            m_selectable_components.Add(m_setting_gfx_ssao);
            m_selectable_components.Add(m_setting_gfx_fps);
            m_selectable_components.Add(m_setting_gfx_fullscreen);
            m_selectable_components.Add(m_menu_Back);

            m_ownerscreen = p_owner;
        }
        #endregion Constructor

        #region Update
        public override void Update(GameTime p_gametime)
        {
            base.Update(p_gametime);

            if (!m_rendersubscreen && m_ready)
            {
                // If mouse have moved, clear keyboard selection
                if (m_lastmouse.X != m_currentmouse.X || m_lastmouse.Y != m_currentmouse.Y)
                {
                    // Hit test all components (only menu items here anyway)
                    foreach (GameScreenComponent t_component in m_selectable_components)
                    {
                        t_component.HitTest(new Vector2(m_currentmouse.X, m_currentmouse.Y));
                    }
                }

                // Setting -> GFX: Motion blur
                if (m_setting_gfx_motion_blur.Active && m_activatetrigger)
                    ((ScreenComponentSwitch)m_setting_gfx_motion_blur).SwitchValue();

                // Setting -> GFX: Bloom effect
                if (m_setting_gfx_bloom.Active && m_activatetrigger)
                    ((ScreenComponentSwitch)m_setting_gfx_bloom).SwitchValue();

                // Setting -> GFX: SSAO effect
                if (m_setting_gfx_ssao.Active && m_activatetrigger)
                    ((ScreenComponentSwitch)m_setting_gfx_ssao).SwitchValue();

                // Setting -> GFX: FPS Counter
                if (m_setting_gfx_fps.Active && m_activatetrigger)
                    ((ScreenComponentSwitch)m_setting_gfx_fps).SwitchValue();

                // Setting -> GFX: Fullscreen
                if (m_setting_gfx_fullscreen.Active && m_activatetrigger)
                    ((ScreenComponentSwitch)m_setting_gfx_fullscreen).SwitchValue();

                // Back?
                if (m_menu_Back.Active && m_activatetrigger)
                {
                    ((Game1)m_game).settingGFXMotionBlur = ((ScreenComponentSwitch)m_setting_gfx_motion_blur).value;
                    ((Game1)m_game).settingGFXBloom = ((ScreenComponentSwitch)m_setting_gfx_bloom).value;
                    ((Game1)m_game).settingGFXSSAO = ((ScreenComponentSwitch)m_setting_gfx_ssao).value;
                    ((Game1)m_game).settingGFXFPS = ((ScreenComponentSwitch)m_setting_gfx_fps).value;
                    ((Game1)m_game).settingGFXFS = ((ScreenComponentSwitch)m_setting_gfx_fullscreen).value;

                    ((Game1)m_game).SaveConfig();

                    this.Close();
                }
            }

            // Store keyboard and mouse states
            StoreStates();
        }
        #endregion Update

        #region Draw
        public override void Draw()
        {
            if (!m_rendersubscreen)
            {
                m_spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, m_screenmatrix);

                // Draw menu components
                foreach (GameScreenComponent t_component in m_selectable_components)
                {
                    t_component.Draw(m_spriteBatch, false);
                }

                // Draw minor titles
                m_spriteBatch.DrawString(m_normalfont, "Graphics", new Vector2(91, 201), Color.Black);
                m_spriteBatch.DrawString(m_normalfont, "Graphics", new Vector2(90, 200), Color.White);

                m_spriteBatch.End();
            }
            else
            {
                m_subscreen.Draw();
            }

            base.Draw();
        }
        #endregion Draw

    }
}
